<template>
  <div ref="dom" class="charts chart-bar"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
import { on, off } from '@/libs/tools'
echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'Shangbao',

  props: {
    value: Array,
    text: String,
    isSolid: Boolean
  },

  data () {
    return {
      dom: null
    }
  },

  watch: {
    value: {
      // immediate: true,
      handler (val, oldVal) {
        this.value = val
        this.initLine()
      }
    }
  },

  mounted () {
    this.$nextTick(() => {
      this.initLine()
    })
  },

  methods: {
    resize () {
      this.dom.resize()
    },

    initLine () {
      let seriesData = this.value.map(item => {
        return { value: item.total, name: item.periodMonth + '月' }
      })
      let option = {
        title: {
          text: this.text,
          x: 'center',
          textStyle: {
            color: '#7391c4',
            fontSize: '16',
            fontWeight: 'normal'
          }
        },
        // grid: {top: 40, bottom: 20, left: 30, right: 20},
        tooltip: {
          trigger: 'item',
          formatter: '{b} :  {c}元'
        },
        series: [
          {
            type: 'pie',
            radius: this.isSolid ? '70%' : ['50%', '70%'],
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            label: {
              normal: {
                show: false
              },
              emphasis: {
                show: true
              }
            },
            lableLine: {
            },
            data: seriesData
          }
        ]
      }
      this.dom = echarts.init(this.$refs.dom, 'tdTheme')
      this.dom.setOption(option)
      on(window, 'resize', this.resize)
    }
  },
  beforeDestroy () {
    off(window, 'resize', this.resize)
  }
}
</script>
